import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;

@SuppressWarnings("serial")
public class GameOptionPanel extends JPanel implements ActionListener {

	private JPanel nupuPaneel, nupuPaneelParem, sisuPaneel;
	private JButton lisa, eemalda, combatMode;
	private int tegelasteMaxArv = 12;
	private int tegelasteArv = 4;
	public String failinimi = "";
	ArrayList<JLabel> tegelasteMahuti = new ArrayList<JLabel>();

	// Stolini muutujad
	private Loomad loomad;
	private int gameOption;
	ArrayList<ArrayList<JCheckBox>> tsekboxid;
	ArrayList<ArrayList<JTextArea>> leibelid;
	ArrayList<ArrayList<JButton>> nupud, ai_nupud, team_nupud;
	ArrayList<ArrayList<JPanel>> vastuserida;

	// Stolini muutujad lypp

	public GameOptionPanel() {
		
		// kogu GameOptionPane seaded
		setLayout(new BorderLayout());

		// Stolini koodijupp
		setGameOption(0);
		loomad = new Loomad();
		// Stolini koodijupp lypp

		// loon nupupaneeli + nupuPaneelParem
		nupuPaneel = new JPanel();
		nupuPaneel.setLayout(new BorderLayout());
		nupuPaneelParem = new JPanel();
		nupuPaneelParem.setLayout(new FlowLayout());

		// teen nupud
		lisa = new JButton(" + lisa tegelane ");
		lisa.addActionListener(this);
		lisa.setEnabled(true);
		eemalda = new JButton(" - eemalda tegelane ");
		eemalda.addActionListener(this);
		eemalda.setEnabled(true);
		combatMode = new JButton("Vali teema");
		combatMode.addActionListener(this);
	

		nupuPaneel.add(nupuPaneelParem, BorderLayout.EAST);
		nupuPaneelParem.add(lisa);
		nupuPaneelParem.add(eemalda);
		nupuPaneelParem.add(combatMode);

		loo_sisu_paneel ();
		
		this.add(nupuPaneel, BorderLayout.NORTH);
		this.add(sisuPaneel, BorderLayout.CENTER);
		
		CardLayout cl = (CardLayout)(sisuPaneel.getLayout());
	    cl.show(sisuPaneel, "Kogum" + tegelasteArv);
		
		// teeN2htavaks();

	}
	
	public void loo_sisu_paneel () {
	
		sisuPaneel = new JPanel();
		sisuPaneel.setLayout(new CardLayout());
		leibelid = new ArrayList<ArrayList<JTextArea>>();
		vastuserida = new ArrayList<ArrayList<JPanel>>();
		nupud= new ArrayList<ArrayList<JButton>> ();
		ai_nupud= new ArrayList<ArrayList<JButton>> ();
		team_nupud = new ArrayList<ArrayList<JButton>> ();
		
		for (int i = 0; i < tegelasteMaxArv + 1; i++) {
	
			ArrayList<JTextArea> uusJLabelid = new ArrayList<JTextArea>();
			ArrayList<JPanel>uusJPanelid = new ArrayList<JPanel>();
			ArrayList<JButton>uusJNupud = new ArrayList<JButton>();
			ArrayList<JButton>uusAiNupud = new ArrayList<JButton>();
			ArrayList<JButton>uusTeamNupud = new ArrayList<JButton>();
			JPanel uus_kogum = new JPanel();
			
			Loom loom = new Loom ();
			loom.setType(i+1);
			loom.ID=i;
			
			if (i==0) {
				loom.is_AI=false;
			} else {
				loom.is_AI=true;
			}
			
			loom.team=i+1;
			loom.nimi = loom.nimi + " " + (i+1);
			loomad.addNewLoom(loom);
			//   uus_kogum.setLayout(new GridLayout(i, 2));
			 uus_kogum.setLayout(new GridLayout(tegelasteMaxArv, 2));
		
			for (int j = 0; j < i; j++) {
				
				JPanel uus_paneel = new JPanel();
				uus_paneel.setLayout(new GridLayout(1,4));
				String kysimus = loomad.loomad.get(j).nimi;
				
				JTextArea uus_label = new JTextArea(kysimus);
				
				// leibelid[i] = new JLabel(kysimus);
				//TODO font dünaamiliseks?
				uus_label.setFont(new Font("Arial",1,16)); // (int)(0.025 * this.getWidth())
				uus_label.setOpaque(true);
				uus_label.setBackground(new Color(209, 238, 238));
				uus_label.setBorder(BorderFactory.createLineBorder(Color.cyan));
				
				uus_paneel.add(uus_label);
				
				JButton uus_nupp = new JButton(loomad.loomad.get(j).getType().toUpperCase());
				uus_nupp.setActionCommand("Muuda loomatüüpi " + j);	
				uus_nupp.addActionListener(this);
				uus_paneel.add(uus_nupp);
				uusJNupud.add(uus_nupp);
				
				
				String nuputekst = "";
				if (loomad.loomad.get(j).is_AI) {
					nuputekst = "AI";
				} else {
					nuputekst = "HUMAN";
				}
				
				JButton uus_AI_nupp = new JButton(nuputekst);
				uus_AI_nupp.setActionCommand("AI_vyi_Human" + j);		
				uus_AI_nupp.addActionListener(this);
				uus_paneel.add(uus_AI_nupp);
				uusAiNupud.add(uus_AI_nupp);
				
				JButton uus_Team_nupp = new JButton("Team " + loomad.loomad.get(j).team);
				uus_Team_nupp.setActionCommand("Change team" + j);		
				uus_Team_nupp.addActionListener(this);
				uus_paneel.add(uus_Team_nupp);
				uusTeamNupud.add(uus_Team_nupp);
	
				uusJLabelid.add(uus_label);
				uusJPanelid.add(uus_paneel);
				uus_kogum.add(uus_paneel);
			}
			
			leibelid.add(uusJLabelid);
			vastuserida.add(uusJPanelid);
			nupud.add(uusJNupud);
			ai_nupud.add(uusAiNupud);
			team_nupud.add(uusTeamNupud);
			sisuPaneel.add(uus_kogum, ("Kogum" + i));
		
			
		}
		
	}
	

	// Stolini meetotid
	public void setLoomad(Loomad loomad) {
		this.loomad = loomad;
	}

	public Loomad getLoomad() {
		return loomad;
	}

	public void setGameOption(int gameOption) {
		this.gameOption = gameOption;
	}

	public int getGameOption() {
		return gameOption;
	}

	// Stolini meetotid lypp

	@Override
	public void actionPerformed(ActionEvent event) {

		String command = event.getActionCommand();

		if (command.contains("+")) {
			tegelasteArv++;	
		    teeN2htavaks();
		}

		if (command.contains("-")) {
			tegelasteArv--;
			teeN2htavaks();

		}

		if ( command.contains("Vali teema")) {
			ArrayList<File> failid = new ArrayList<File>();
			File actual = new File(".");
			for (File f : actual.listFiles()) {
				if (f.isFile()) {
					if (f.toString().contains(".xml"))
						failid.add(f);
				}
			}
			Object[] selectionValues = new Object[failid.size()];
			for (int i = 0; i < failid.size(); i++) {
				selectionValues[i] = failid.get(i).getName();
			}
			Object selection = JOptionPane.showInputDialog(null,
					"Vali küsimuste-vastuste fail!", "Teemavalik",
					JOptionPane.QUESTION_MESSAGE, null, selectionValues, 3);
			if(selection == null)
				return;
			failinimi = selection.toString();
		}
		
		if ( command.contains("AI_vyi_Human")) {
			int looma_nr_arrays = get_looma_nr(command);
			Loom loom = loomad.loomad.get(looma_nr_arrays);
			String nuputekst = "";
			if (loom.is_AI) {
				loom.is_AI=false;
				nuputekst = "HUMAN";
			} else {
				loom.is_AI=true;
				nuputekst = "AI";
			}
			for (int i = 0; i <  ai_nupud.size(); i++) {
					if (ai_nupud.get(i).size()> looma_nr_arrays) {
						ai_nupud.get(i).get(looma_nr_arrays).setText(nuputekst);
					}
				
			}
		} // end if ( command.contains("Muuda AI")) {
		if ( command.contains("Change team")) {
			int looma_nr_arrays = get_looma_nr(command);
			Loom loom = loomad.loomad.get(looma_nr_arrays);
			
			Object[] selectionValues = new Object[tegelasteMaxArv];
			for (int i = 0; i <  tegelasteMaxArv; i++) {
				selectionValues[i] = i+1;
			}
			Object selection = JOptionPane.showInputDialog(null,
					"Vali team!", "Teamivalik",	JOptionPane.QUESTION_MESSAGE, null, selectionValues, loom.team);
			if(selection == null)
				return;
			loom.team=Integer.valueOf(selection.toString());
			
			for (int i = 0; i <  team_nupud.size(); i++) {
				if (team_nupud.get(i).size()> looma_nr_arrays) {
					
					team_nupud.get(i).get(looma_nr_arrays).setText("Team " + loom.team);
	
				}
			}
			
		}
		
		if ( command.contains("Muuda loomatüüpi")) {
			
			
			int looma_nr_arrays = get_looma_nr(command);
			Loom loom = loomad.loomad.get(looma_nr_arrays);
			
			int eri_loomatyype = loom.erinevaid_loomatyype;
			
			Object[] selectionValues = new Object[eri_loomatyype];
			for (int i = 0; i <  eri_loomatyype; i++) {
				selectionValues[i] = loom.returnAnyType(i+1);
			}
			Object selection = JOptionPane.showInputDialog(null,
					"Vali loom!", "Loomavalik",	JOptionPane.QUESTION_MESSAGE, null, selectionValues, 3);
			if(selection == null)
				return;
			
			loom.setType(selection.toString());
			
			for (int i = 0; i <  nupud.size(); i++) {
					if (nupud.get(i).size()> looma_nr_arrays) {
						nupud.get(i).get(looma_nr_arrays).setText(loom.getType().toUpperCase());
						leibelid.get(i).get(looma_nr_arrays).setText(loom.nimi + " " + (looma_nr_arrays + 1));
					}
				
			}
			
		} // end if ( command.contains("Muuda loomatyypi")) {

	}
	
	public int get_looma_nr(String command) {
		String mitmes_loom_String = "";
		
		for (int i = 0; i <  command.length(); i++) {
			if (Character.isDigit(command.charAt(i))) {
				mitmes_loom_String= mitmes_loom_String + command.charAt(i); 
			}
		}
		
		return Integer.parseInt(mitmes_loom_String);
	}
	public void teeN2htavaks() {

		CardLayout cl = (CardLayout)(sisuPaneel.getLayout());
	    cl.show(sisuPaneel, "Kogum" + tegelasteArv);
	    
		muudaNupuStaatus();

	}// teeN2htavaks

	public int gettegelasteArv() {
		return this.tegelasteArv;
	}
	public void muudaNupuStaatus() {

		lisa.setEnabled(true);
		eemalda.setEnabled(true);

		if (tegelasteArv >= tegelasteMaxArv) {
			lisa.setEnabled(false);
		}

		else if (tegelasteArv < 3) {
			eemalda.setEnabled(false);
		}

	}//muudaNupuStaatus

	public void muudaNuppN2htamatuks(){
		lisa.setEnabled(false);
		eemalda.setEnabled(false);
	}
	
	public void joonistaCombatMode(){
		
		//siia joonistame combat mode valimise menüü
		
	}

	public void puhastaEkraan(){
		sisuPaneel.removeAll();
		sisuPaneel.repaint();
	}
	
}// pea klass
